package shm;

import javax.swing.*;
import javax.swing.event.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

/* 
 * This class is responsible for Online display 
 * Originally copied from Intel Demo.
 * */
public class Window {

	public static int currentID = 0;

	// UI elements
	public static JPanel main;
	
	JLabel xLabel;
	JTextField sampleText, yText;
	public static JFrame frame;
	JLabel idLabel;
	public static JLabel real_seq = new JLabel("0");
	public static JSlider slider_ymin, slider_ymax;
	public static int Ymax = 65535;
	public static int Ymin = 0;
	public static JTextField jta_min, jta_max;
	public JLabel sampleLabel;
	public static JComboBox cbId;
	
	// chart
	public static JFreeChart jfc;
	static ChartPanel cp;
	static XYSeriesCollection dataset;
	
	void setup() {
		main = new JPanel();
		main.setBounds(0, 0, 800, 710);
		main.setLayout(null);
		
		jfc = createChart();		
		cp = new ChartPanel(jfc);
		cp.setBounds(0, 0, 705, 393);
		main.add(cp);

		JLabel label_seq = new JLabel("Seq");
		label_seq.setBounds(45, 390, 50, 40);
		main.add(label_seq);

		real_seq.setBounds(95, 390, 50, 40);

		main.add(real_seq);

		JLabel lab_node = new JLabel("Node");

		lab_node.setBounds(145, 390, 50, 40);
		main.add(lab_node);
		cbId = new JComboBox();
		cbId.setBounds(195, 395, 60, 30);
		main.add(cbId);
		cbId.addItem("All");
		for (int i = 1; i < 30; i++)
			cbId.addItem(String.valueOf(i));
		
		cbId.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent evt) {
				if (evt.getStateChange() == ItemEvent.SELECTED) {
					Window.currentID = cbId.getSelectedIndex();	
				}
			}
		});

		JLabel x_label = new JLabel("X: ");
		x_label.setBounds(13, 440, 20, 20);
		main.add(x_label);
		final JSlider slider = new JSlider(JSlider.HORIZONTAL);
		slider.setMinimum(0);
		slider.setMajorTickSpacing(50);
		slider.setMaximum(8);

		slider.setBounds(30, 430, 190, 50);
		Hashtable<Integer, JLabel> xTable = new Hashtable<Integer, JLabel>();

		for (int i = 0; i <= 8; i += 2) {
			xTable.put(new Integer(i), new JLabel("" + (10 << i)));
		}
		slider.setLabelTable(xTable);
		slider.setPaintLabels(true);
		//graph.setScale(graph.scale);

		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				//graph.setScale((int) slider.getValue());
			}
		});
		main.add(slider);

		slider_ymin = new JSlider();
		slider_ymin.setOrientation(JSlider.HORIZONTAL);
		slider_ymin.setMinimum(0);
		slider_ymin.setMaximum(65);
		slider_ymin.setMajorTickSpacing(10);
		slider_ymin.setMinorTickSpacing(5);
		slider_ymin.setPaintLabels(true);
		slider_ymin.setPaintTicks(false);
		slider_ymin.setPaintTrack(true);// Determines whether the track is
										// painted on the slider
		slider_ymin.setValue(0);
		slider_ymin.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jta_min.setText((int) slider_ymin.getValue() * 1000 + "");
				Window.Ymin = (int) slider_ymin.getValue() * 1000;
				if (Window.Ymin > 65535) {
					Window.Ymin = 65535;
				}
				if (Window.Ymin < Window.Ymax) {
					//graph.setYAxis(Window.Ymin, Window.Ymax);
				}
			}
		});
		slider_ymin.setBounds(320, 395, 190, 50);

		main.add(slider_ymin);
		JLabel y_min_label = new JLabel("Ymin: ");

		y_min_label.setBounds(270, 400, 40, 20);
		main.add(y_min_label);
		JLabel y_max_label = new JLabel("Ymax: ");
		y_max_label.setBounds(270, 450, 40, 20);
		main.add(y_max_label);
		slider_ymax = new JSlider();
		slider_ymax.setOrientation(JSlider.HORIZONTAL);
		slider_ymax.setMinimum(0);
		slider_ymax.setMaximum(65);
		slider_ymax.setMajorTickSpacing(10);
		slider_ymax.setMinorTickSpacing(5);
		slider_ymax.setPaintLabels(true);
		slider_ymax.setPaintTicks(false);
		slider_ymax.setPaintTrack(true);// Determines whether the track is
										// painted on the slider
		slider_ymax.setValue(65);

		slider_ymax.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				jta_max.setText((int) slider_ymax.getValue() * 1000 + "");
				Window.Ymax = (int) slider_ymax.getValue() * 1000;
				if (Window.Ymax > 65535) {
					Window.Ymax = 65535;
				}
				if (Window.Ymin < Window.Ymax) {
					//graph.setYAxis(Window.Ymin, Window.Ymax);
				}
			}
		});

		slider_ymax.setBounds(320, 445, 190, 50);
		main.add(slider_ymax);
		jta_min = new JTextField("0");
		jta_min.setBounds(525, 395, 50, 26);
		main.add(jta_min);

		jta_max = new JTextField("65535");

		jta_max.setBounds(525, 445, 50, 26);
		main.add(jta_max);
	}

	private final static int MAXSIZE = 128;
	public HashMap<Integer, Queue<Integer>> data = new HashMap<Integer, Queue<Integer>>();
	private long refreshTic = 0;
	
    public void newData(int nodeId, int seq, int x, int y, int z, int e){

		if(! data.containsKey(nodeId)) {
			LinkedList<Integer> ll = new LinkedList<Integer>();
			for (int i = 0; i< MAXSIZE; i++)
				ll.add(0);
			
			data.put(nodeId,  ll);
		}
		
		Queue<Integer> queue = data.get(nodeId);
		
		queue.poll();
		queue.add(x);
		
		// rebuild dataset
		long curTic = System.currentTimeMillis();
		if ( curTic - refreshTic < 100)
			return;
		
		refreshTic = curTic;
		
		dataset.removeAllSeries();
		
		for(Integer i : data.keySet()) {
			XYSeries s = new XYSeries(i.toString());
			
			if (currentID == i || currentID == 0) {
				Queue<Integer> q = data.get(i);
				Integer[] arr = new Integer[MAXSIZE + 1];
				arr = q.toArray(arr);
				for (int j = 0; j < arr.length; j++) {
					s.add(j, arr[j]);
				}
			}
			dataset.addSeries(s);
		}
    }
    
	public static JFreeChart createChart() {
		
		dataset = new XYSeriesCollection();

		JFreeChart jfreechart = ChartFactory.createXYLineChart("", 
				"", 
				"",
				dataset,
				PlotOrientation.VERTICAL,
				true, true, true);

		XYPlot plot = (XYPlot) jfreechart.getPlot();
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		ValueAxis domain = plot.getDomainAxis();
		domain.setTickLabelPaint(Color.white);
		ValueAxis range = plot.getRangeAxis();
		range.setTickLabelPaint(Color.white);

		jfreechart.clearSubtitles();
		jfreechart.setBackgroundPaint(Color.black);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		xyplot.setBackgroundPaint(Color.black);

		ValueAxis ca = xyplot.getRangeAxis();
		ca.setAutoRange(true);
		ValueAxis ca2 = xyplot.getDomainAxis();
		ca2.setAutoRange(true);
		plot.setBackgroundAlpha(0.5f);
		plot.setForegroundAlpha(0.5f);
		
		XYItemRenderer renderer = plot.getRenderer();
		
		Stroke stk = new BasicStroke(2f);
		
		renderer.setSeriesStroke(0, stk);
		renderer.setSeriesStroke(1, stk);
		renderer.setSeriesStroke(2, stk);
		renderer.setSeriesStroke(3, stk);
		renderer.setSeriesPaint(0, Color.YELLOW);
		renderer.setSeriesPaint(1, Color.RED);
		renderer.setSeriesPaint(2, Color.CYAN);
		renderer.setSeriesPaint(3, Color.BLUE);

		return jfreechart;
	}
	
}
